package com.demo.studentmanager.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.studentmanager.dto.ScoreAddDto;
import com.demo.studentmanager.dto.ScoreListDto;
import com.demo.studentmanager.dto.ScoreUpdDto;
import com.demo.studentmanager.entity.Score;
import com.demo.studentmanager.entity.Teach;
import com.demo.studentmanager.exception.BizException;
import com.demo.studentmanager.result.ResultCode;
import com.demo.studentmanager.service.ICourseService;
import com.demo.studentmanager.service.IScoreService;
import com.demo.studentmanager.service.IStudentService;
import com.demo.studentmanager.util.CheckEntityUtil;
import com.demo.studentmanager.vo.ScoreVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author LinQiSen
 */
@Api(tags = "学生成绩模块")
@RequestMapping("/score")
@RestController
public class ScoreController {
    @Autowired
    private IScoreService scoreService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IStudentService studentService;

    @RequiresAuthentication
    @GetMapping
    @ApiOperation(value = "查询学生成绩信息")
    IPage<ScoreVO> getScoreList(@Validated ScoreListDto scoreListDto) {
        return scoreService.getScorelist(scoreListDto);
    }


    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @PostMapping
    @ApiOperation(value = "增加学生成绩信息")
    void addScore(@Validated ScoreAddDto scoreAddDto) {
        CheckEntityUtil.isNullById(scoreAddDto.getSno(),studentService,ResultCode.STUDENT_NOT_EXIST);
        CheckEntityUtil.isNullById(scoreAddDto.getCno(),courseService,ResultCode.EXAM_ACCOUNT_NOT_EXIST);
        Score score = new Score().setSno(scoreAddDto.getSno()).setCno(scoreAddDto.getCno());
        Wrapper<Score> queryWrapper = new QueryWrapper<>(score);
        if (scoreService.getOne(queryWrapper) != null) {
            throw new BizException(ResultCode.SCORE_EXIST);
        }

        score = new Score();
        scoreAddDto.convertToEntity(score);
        scoreService.save(score);
    }

    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @PutMapping
    @ApiOperation(value = "修改学生成绩信息")
    void upScore(@Validated ScoreUpdDto scoreUpdDto) {

        Score score = CheckEntityUtil.isNullById(scoreUpdDto.getId(), scoreService, ResultCode.STUDENT_NOT_EXIST);
        score.setDegree(scoreUpdDto.getDegree());
        scoreService.updateById(score);
    }

    @RequiresRoles(value={"teacher","admin"},logical= Logical.OR)
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除成绩信息")
    void delScore(@NotNull @PathVariable Integer id) {
        CheckEntityUtil.isNullById(id, scoreService, ResultCode.SCORE_NOT_EXIST);

        scoreService.removeById(id);
    }
}
